﻿using System.Data.SqlClient;

namespace ErrorDumper.PropertyBinding {
    public class SqlExceptionPropertyBinder : IExceptionPropertyBinder {
        public ExceptionPropertyCollection BindProperties(
            ExceptionPropertyCollection properties, object instance) {

            var exception = instance as SqlException;
            if (exception == null)
                return null;

            if (properties == null)
                properties = new ExceptionPropertyCollection();
            if (!properties.Contains("SqlErrorCode"))
                properties["SqlErrorCode"] = exception.ErrorCode.ToString();
            if (!properties.Contains("SqlErrorClass"))
                properties["SqlErrorClass"] = exception.Class.ToString();
            if (!properties.Contains("SqlErrorLineNumber"))
                properties["SqlErroLineNumber"] = exception.LineNumber.ToString();
            if (!properties.Contains("SqlErrorNumber"))
                properties["SqlErrorNumber"] = exception.Number.ToString();
            if (!string.IsNullOrEmpty(exception.Procedure) && !properties.Contains("SqlErrorProcedure"))
                properties["SqlErrorProcedure"] = exception.Procedure;
            if (!properties.Contains("SqlErrorServer"))
                properties["SqlErrorServer"] = exception.Server;
            if (!properties.Contains("SqlErrorState"))
                properties["SqlErrorState"] = exception.State.ToString();
            return properties;
        }
    }
}